/**
    File        : EditAccountDlg.cpp
    Description : Dialog class for creating / editing accounts
    Author      : Raymond Marsh
    Created     : 08/01/2009
    Copyright 2009, 2010, 2011, 2012 Raymond Marsh

    This program is distributed under the terms of the GNU General Public License.
    To view a copy of the license see the file 'copying.txt'

    This file is part of the MyMoney Suite.

    MyMoney is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free Software Foundation, either
    version 3 of the License, or (at your option) any later version.

    MyMoney is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with MyMoney.
    If not, see <http://www.gnu.org/licenses/>.
**/

#include "..\include\EditAccountDlg.h"

//(*InternalHeaders(CEditAccountDlg)
#include <wx/xrc/xmlres.h>
//*)

//(*IdInit(CEditAccountDlg)
//*)

BEGIN_EVENT_TABLE(CEditAccountDlg,wxDialog)
	//(*EventTable(CEditAccountDlg)
	//*)
END_EVENT_TABLE()

CEditAccountDlg::CEditAccountDlg(wxWindow* parent, SAccountInfo& pAccountInfo, wxArrayString& AccountTypes, const bool CanEdit, const bool IsNewAccount)
: m_IsNewAccount(IsNewAccount), m_CanEdit(CanEdit)
{
	//(*Initialize(CEditAccountDlg)
	wxXmlResource::Get()->LoadObject(this,parent,_T("CEditAccountDlg"),_T("wxDialog"));
	StaticText1 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT1"));
	TextCtrl1 = (wxTextCtrl*)FindWindow(XRCID("id_AccountNameCtrl"));
	StaticText2 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT2"));
	TextCtrl2 = (wxTextCtrl*)FindWindow(XRCID("id_AccountNumberCtrl"));
	StaticText3 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT3"));
	ComboBox1 = (wxComboBox*)FindWindow(XRCID("id_AccountTypeCtrl"));
	StaticText4 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT4"));
	TextCtrl3 = (wxTextCtrl*)FindWindow(XRCID("id_BankNameCtrl"));
	StaticText5 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT5"));
	TextCtrl4 = (wxTextCtrl*)FindWindow(XRCID("id_BankAddressCtrl"));
	StaticText6 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT6"));
	TextCtrl5 = (wxTextCtrl*)FindWindow(XRCID("id_BankPhoneCtrl"));
	StaticText7 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT7"));
	DatePickerCtrl1 = (wxDatePickerCtrl*)FindWindow(XRCID("id_DateOpenedCtrl"));
	StaticText8 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT8"));
	TextCtrl6 = (wxTextCtrl*)FindWindow(XRCID("id_OpeningBalanceCtrl"));
	StaticText9 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT9"));
	DatePickerCtrl2 = (wxDatePickerCtrl*)FindWindow(XRCID("id_LastStatementDateCtrl"));
	StaticText10 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT10"));
	TextCtrl7 = (wxTextCtrl*)FindWindow(XRCID("id_LastStatementBalanceCtrl"));
	StaticText11 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT11"));
	TextCtrl8 = (wxTextCtrl*)FindWindow(XRCID("id_CurrentBalanceCtrl"));
	StaticText12 = (wxStaticText*)FindWindow(XRCID("ID_STATICTEXT12"));
	StaticLine1 = (wxStaticLine*)FindWindow(XRCID("ID_STATICLINE1"));
	Button1 = (wxButton*)FindWindow(XRCID("id_BtnOK"));
	Button2 = (wxButton*)FindWindow(XRCID("id_BtnCancel"));
	//*)

	m_pAccountTypeCtrl->Set(AccountTypes);

    // disable controls if needed
    if (!m_IsNewAccount)
    {
        m_pDateOpenedCtrl->Disable();
        m_pOpeningBalanceCtrl->Disable();
        if (!m_CanEdit)
        {
            m_pAccountNameCtrl->Disable();
            m_pAccountNumberCtrl->Disable();
            m_pAccountTypeCtrl->Disable();
            m_pBankNameCtrl->Disable();
            m_pBankAddressCtrl->Disable();
            m_pBankPhoneCtrl->Disable();
        }
    }

 	// set validators
	m_pAccountNameCtrl->SetValidator(wxTextValidator(wxFILTER_NONE, &pAccountInfo.AccountName) );
    m_pAccountNumberCtrl->SetValidator(wxTextValidator(wxFILTER_NONE, &pAccountInfo.AccountNumber) );
    m_pAccountTypeCtrl->SetValidator(wxTextValidator(wxFILTER_NONE, &pAccountInfo.AccountType) );
    m_pBankNameCtrl->SetValidator(wxTextValidator(wxFILTER_NONE, &pAccountInfo.BankName) );
    m_pBankAddressCtrl->SetValidator(wxTextValidator(wxFILTER_NONE, &pAccountInfo.BankAddress) );
    m_pBankPhoneCtrl->SetValidator(wxPhoneValidator(wxUSE_HYPHENS, &pAccountInfo.BankPhone) );
    m_pDateOpenedCtrl->SetValidator(wxDateTimeValidator(wxUSE_SLASHES, &pAccountInfo.DateOpened) );
    m_pOpeningBalanceCtrl->SetValidator(wxFloatingPointValidator<double>(2, &pAccountInfo.OpeningBalance, wxNUM_VAL_ZERO_AS_BLANK) );
    m_pLastStatementDateCtrl->SetValidator(wxDateTimeValidator(wxUSE_SLASHES, &pAccountInfo.LastStatementDate) );
    m_pLastStatementBalanceCtrl->SetValidator(wxFloatingPointValidator<double>(2, &pAccountInfo.LastStatementBalance, wxNUM_VAL_ZERO_AS_BLANK) );
    m_pCurrentBalanceCtrl->SetValidator(wxFloatingPointValidator<double>(2, &pAccountInfo.CurrentBalance, wxNUM_VAL_ZERO_AS_BLANK) );
}

CEditAccountDlg::~CEditAccountDlg()
{
	//(*Destroy(CEditAccountDlg)
	//*)
}


void CEditAccountDlg::OnBtnOKClick(wxCommandEvent& event)
{
    if (m_CanEdit)
    {
        // check that required fields have data
        if (m_pAccountNameCtrl->IsEmpty() || m_pAccountNumberCtrl->IsEmpty() ||
            m_pAccountTypeCtrl->GetSelection() == wxNOT_FOUND || m_pOpeningBalanceCtrl->IsEmpty() )
        {
            mmMessageDlg(this, _("Required field(s) are empty!") );
            return;
        }
        if (m_IsNewAccount)
        {
            m_pLastStatementDateCtrl->SetValue(m_pDateOpenedCtrl->GetValue() );
            m_pLastStatementBalanceCtrl->SetValue("0");
            m_pCurrentBalanceCtrl->SetValue(m_pOpeningBalanceCtrl->GetValue() );
        }
        if (this->Validate() )
            this->TransferDataFromWindow();
    }
    if (this->IsModal() )
        this->EndModal(wxID_OK);
    else
    {
        this->SetReturnCode(wxID_OK);
        this->Show(false);
    }
}

void CEditAccountDlg::OnBtnCancelClick(wxCommandEvent& event)
{
    if (this->IsModal() )
        this->EndModal(wxID_CANCEL);
    else
    {
        this->SetReturnCode(wxID_CANCEL);
        this->Show(false);
    }
}

